package com.han.spring.mvc._0143_Handler_Methods;

import java.io.IOException;
import java.io.InputStream;
import java.security.Principal;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* @author hanpeng
* @version 创建时间：2020年8月2日 下午2:53:06
* @ClassName 类名称
* @Description 类描述
*/
@Controller
public class MethodArgumentController {
	private  static Log log = LogFactory.getLog(MethodArgumentController.class);
	private  static final String SUCCESS="success";
	/*WebRequest, NativeWebRequest*/
	@RequestMapping(value="/webRequest")
	public String webRequest(NativeWebRequest request) {
		return SUCCESS;
	}
	//javax.servlet.ServletRequest 
	//javax.servlet.ServletResponse
	//ServletRequest, HttpServletRequest, or Spring’s MultipartRequest, MultipartHttpServletRequest.
	//javax.servlet.http.HttpSession  //session 如果不存在 会强制创建一个session
	@RequestMapping(value="/httpServletRequest")
	public String httpServletRequest(HttpServletRequest request) {
		String[] parameterValues = request.getParameterValues("");
		
		return SUCCESS;
	}
	//java.security.Principal
	@RequestMapping("/principal")
	public String httpServletRequest(Principal principal) {
		System.out.println("principal"+principal);
		return SUCCESS;
	}

	// java.security.Principal
	@RequestMapping("/httpMethod")
	public String httpMethod(HttpMethod httpMethod) {
		System.out.println("principal" + httpMethod);
		return SUCCESS;
	}
	@RequestMapping("/locale")
	public String locale(Locale locale,InputStream is) throws IOException {
		System.out.println("principal" + locale);
		int available = is.available();
		System.out.println(available);
		return SUCCESS;
	}
	@RequestMapping("/pathVariable/{userName}")
	public String pathVariable(@PathVariable(value="userName") String name)  {
		System.out.println(name);
		return SUCCESS;
	}
	//springWebMVC/matrixVariable/42;q=11;r=12/34;q=22,23;s=23
	//文档中的segment 指的是url中/ /之间的 如/42;q=11;r=12/   以及/34;q=22;s=23
	//这个注解的开启方式 <mvc:annotation-driven enable-matrix-variables="true" />
	@RequestMapping("/matrixVariable/{userId}/{petId}")
	public String matrixVariable(@MatrixVariable(pathVar="userId") Map<String,String> mapUserId,
						 @MatrixVariable Map<String,String> allMap,
						 @PathVariable(value="userId") String  userId,
						 @MatrixVariable(pathVar="petId",value="q") String[] pets
						 
			)  {
		System.out.println(mapUserId);
		System.out.println(allMap);
		System.out.println(userId);
		System.out.println(pets.length);
		return SUCCESS;
	}
	//  /springWebMVC/requestParam?name=131
	@RequestMapping("/requestParam")
	public String requestParam(@RequestParam(value = "name") int userId) {
		log.info(userId);
		return SUCCESS;
	}
	// User user 支持级联属性
/*	<form action="springmvc/testPojo" method="post">
	username: <input type="text" name="username"/>
	<br>
	password: <input type="password" name="password"/>
	<br>
	email: <input type="text" name="email"/>
	<br>
	age: <input type="text" name="age"/>
	<br>
	city: <input type="text" name="address.city"/>
	<br>
	province: <input type="text" name="address.province"/>
	<br>
	<input type="submit" value="Submit"/>
</form>*/
	@RequestMapping("/requestBody")
	public String requestBody(@RequestBody User user) {
		//@RequestBody 
		log.info("===>"+user);
		return SUCCESS;
	}
	@RequestMapping("/httpEntity")
	public String httpEntity(HttpEntity<User> httpEntity) {
		//@RequestBody 
		log.info("===>"+httpEntity.getBody());
		return SUCCESS;
	}
	//如果不加 @RequestParam  是无法接收入参的  真实类型为BindingAwareModelMap 其作用和model 一样
	//@RequestParam Map<String, String> map  会结束参数 类型为LinkedHashMap  其值不会带到其它页面
	@RequestMapping("/map")
	public String map(@RequestParam Map<String, String> map) {
		//@RequestBody 
		log.info("===>"+map);
		System.out.println(map.getClass().getName());
		return SUCCESS;
	}
	@RequestMapping("/model")
	public String model(Model model) {
		//@RequestBody 
		log.info("===>"+model);
		System.out.println(model.getClass().getName());
		return SUCCESS;
	}
	@RequestMapping("/redirectAttributes")
	public String redirect(User user,RedirectAttributes redirectAttributes) {
		//@RequestBody 
		log.info("===>"+user);
		System.out.println(user.getClass().getName());
		/*redirectAttributes.addAttribute("name","dad" );*/
		redirectAttributes.addFlashAttribute("name", "dad");
		return "redirect:success";
	}
	@RequestMapping("/success")
	public String success( User user) {
		//@RequestBody 
		log.info("===>"+user);
		System.out.println(user.getClass().getName());
		return SUCCESS;
	}
}








